home *** CD-ROM | disk | FTP | other *** search
/ Winzipper / Winzipper_ISO.iso / programming / oracle7 7.2 / DB / UTIL72 / DBMSSQL.SQL < prev    next >
Encoding:
Text File  |  1995-05-09  |  24.1 KB  |  539 lines

  1. rem 
  2. rem $Header: dbmssql.sql 7020200.1 95/02/15 18:31:02 cli Generic<base> $ 
  3. rem 
  4. Rem  Copyright (c) 1991 by Oracle Corporation 
  5. Rem    NAME
  6. Rem      dbmssql.sql - DBMS package for dynamic SQL
  7. Rem   DESCRIPTION
  8. Rem     This package provides a means to use dynamic SQL to access
  9. Rem     the database.
  10. Rem   NOTES
  11. Rem     The procedural option is needed to use this package.
  12. Rem     This package must be created under SYS.
  13. Rem     The operations provided by this package are performed under the current
  14. Rem     calling user, not under the package owner SYS. The old file name
  15. Rem     for this package was dbms_sql.sql.
  16. Rem
  17. Rem     
  18. Rem   MODIFIED     (MM/DD/YY)
  19. Rem     hjakobss   01/17/95 -  bug 260726 - externalize long functionality
  20. Rem     hjakobss   12/15/94 -  bug 255228 - remove misleading comment
  21. Rem     adowning   03/29/94 -  merge changes from branch 1.4.710.4
  22. Rem     adowning   02/02/94 -  split file into public / private binary files
  23. Rem     dsdaniel   01/18/94 -  merge changes from branch 1.4.710.3
  24. Rem     hjakobss   01/06/94 -  merge changes from branch 1.4.710.2
  25. Rem     dsdaniel   12/27/93 -  create dbms_sys_sql package for parse as user
  26. Rem     hjakobss   12/10/93 -  support array parse and mlslabel
  27. Rem     hjakobss   12/09/93 -  merge changes from branch 1.4.710.1
  28. Rem     hjakobss   10/26/93 -  appease marketing
  29. Rem     hjakobss   07/06/93 -  add new datatypes
  30. Rem     hjakobss   06/17/93 -  add get_rpi_cursor
  31. Rem     hjakobss   05/10/93 -  Merge from 7.0.14 
  32. Rem     hjakobss   05/07/93 -  change procedure names 
  33. Rem     hjakobss   04/02/93 -  Branch_for_the_patch 
  34. Rem     hjakobss   01/28/93 -  Add new features 
  35. Rem     jwijaya    01/11/93 -  merge changes from branch 1.1.312.1 
  36. Rem     jwijaya    01/11/93 -  bug 139792 
  37. Rem     jwijaya    10/21/92 -  Creation 
  38.  
  39. REM  ************************************************************
  40. REM  THIS PACKAGE MUST NOT BE MODIFIED BY THE CUSTOMER.  DOING SO
  41. REM  COULD CAUSE INTERNAL ERRORS AND CORRUPTIONS IN THE RDBMS.
  42. REM  ************************************************************
  43.  
  44. REM  ***************************************
  45. REM  THIS PACKAGE MUST BE CREATED UNDER SYS.
  46. REM  ***************************************
  47.  
  48. REM  ***********************************************************
  49. REM  PROCEDURES AND FUNCTIONS IN THIS PACKAGE ARE EXECUTED UNDER
  50. REM  THE CURRENT CALLING USER, NOT UNDER THE PACKAGE OWNER SYS.
  51. REM  ***********************************************************
  52.  
  53.  
  54. create or replace package dbms_sql is
  55.  
  56.   ------------
  57.   --  OVERVIEW
  58.   --
  59.   --  This package provides a means to use dynamic SQL to access the database.
  60.   --
  61.  
  62.   -------------------------
  63.   --  RULES AND LIMITATIONS
  64.   --
  65.   --  Bind variables of a SQL statement are identified by their names.
  66.   --  When binding a value to a bind variable, the string identifying
  67.   --  the bind variable in the statement may optionally contain the
  68.   --  leading colon. For example, if the parsed SQL statement is
  69.   --  "SELECT ENAME FROM EMP WHERE SAL > :X", on binding the variable
  70.   --  to a value, it can be identified using either of the strings ':X'
  71.   --  and 'X'.
  72.   --
  73.   --  Columns of the row being selected in a SELECT statement are identified
  74.   --  by their relative positions (1, 2, 3, ...) as they appear on the select
  75.   --  list from left to right.
  76.   --  
  77.   --  Privileges are associated with the caller of the procedures/functions
  78.   --  in this package as follows:
  79.   --    If the caller is an anonymous PL/SQL block, the procedures/functions
  80.   --    are run using the privileges of the current user.
  81.   --    If the caller is a stored procedure, the procedures/functions are run
  82.   --    using the privileges of the owner of the stored procedure.
  83.   --
  84.   --  WARNING: Using the package to dynamically execute DDL statements can 
  85.   --  results in the program hanging. For example, a call to a procedure in a 
  86.   --  package will result in the package being locked until the execution 
  87.   --  returns to the user side. Any operation that results in a conflicting 
  88.   --  lock, such as dynamically trying to drop the package, before the first 
  89.   --  lock is released will result in a hang. 
  90.   --
  91.   --  The flow of procedure calls will typically look like this:
  92.   --
  93.   --                      -----------
  94.   --                    | open_cursor |
  95.   --                      -----------
  96.   --                           |
  97.   --                           |
  98.   --                           v
  99.   --                         -----
  100.   --          ------------>| parse |
  101.   --         |               -----
  102.   --         |                 |
  103.   --         |                 |---------
  104.   --         |                 v         |
  105.   --         |           --------------  |
  106.   --         |-------->| bind_variable | |
  107.   --         |     ^     -------------   |
  108.   --         |     |           |         |
  109.   --         |      -----------|         |
  110.   --         |                 |<--------
  111.   --         |                 v
  112.   --         |               query?---------- yes ---------
  113.   --         |                 |                           |
  114.   --         |                no                           |
  115.   --         |                 |                           |
  116.   --         |                 v                           v
  117.   --         |              -------                  -------------
  118.   --         |----------->| execute |            ->| define_column |
  119.   --         |              -------             |    -------------
  120.   --         |                 |------------    |          |
  121.   --         |                 |            |    ----------|
  122.   --         |                 v            |              v
  123.   --         |           --------------     |           -------
  124.   --         |       ->| variable_value |   |  ------>| execute |
  125.   --         |      |    --------------     | |         -------
  126.   --         |      |          |            | |            |
  127.   --         |       ----------|            | |            |
  128.   --         |                 |            | |            v
  129.   --         |                 |            | |        ----------
  130.   --         |                 |<-----------  |----->| fetch_rows |
  131.   --         |                 |              |        ----------
  132.   --         |                 |              |            |
  133.   --         |                 |              |            v
  134.   --         |                 |              |    --------------------
  135.   --         |                 |              |  | column_value         |
  136.   --         |                 |              |  | variable_value       |
  137.   --         |                 |              |    ---------------------
  138.   --         |                 |              |            |
  139.   --         |                 |<--------------------------
  140.   --         |                 |
  141.   --          -----------------|
  142.   --                           |
  143.   --                           v
  144.   --                      ------------
  145.   --                    | close_cursor |
  146.   --                      ------------ 
  147.   --
  148.    ---------------
  149.  
  150.   -------------
  151.   --  CONSTANTS
  152.   --
  153.   v6 constant integer := 0;
  154.   native constant integer := 1;
  155.   v7 constant integer := 2;
  156.   --
  157.   --------------
  158.   --  EXCEPTIONS
  159.   --
  160.   inconsistent_type exception;
  161.     pragma exception_init(inconsistent_type, -6562);
  162.   --  This exception is raised by procedure "column_value" or
  163.   --  "variable_value" if the type of the given out argument where
  164.   --  to put the requested value is different from the type of the value.
  165.  
  166.   ----------------------------
  167.   --  PROCEDURES AND FUNCTIONS
  168.   --
  169.   function open_cursor return integer;
  170.   --  Open a new cursor.
  171.   --  When no longer needed, this cursor MUST BE CLOSED explicitly by
  172.   --  calling "close_cursor".
  173.   --  Return value:
  174.   --    Cursor id number of the new cursor.
  175.   --
  176.   function is_open(c in integer) return boolean;
  177.   --  Return TRUE is the given cursor is currently open.
  178.   --  Input parameters:
  179.   --    c
  180.   --      Cursor id number of the cursor to check.
  181.   --  Return value:
  182.   --    TRUE if the given cursor is open,
  183.   --    FALSE if it is not.
  184.   --
  185.   procedure close_cursor(c in out integer);
  186.   --  Close the given cursor.
  187.   --  Input parameters:
  188.   --    c
  189.   --      Cursor id number of the cursor to close.
  190.   --  Output parameters:
  191.   --    c
  192.   --      Will be nulled.
  193.   --
  194.   procedure parse(c in integer, statement in varchar2, 
  195.                   language_flag in integer);
  196.   --  Parse the given statement in the given cursor. NOTE THAT PARSING AND
  197.   --  EXECUTING DDL STATEMENTS CAN CAUSE HANGS! 
  198.   --  Currently, the deferred parsing feature of the Oracle  Call Interface
  199.   --  is not used. As a result, statements are parsed immediately. In addition,
  200.   --  DDL statements are executed immediately when parsed. However, 
  201.   --  the behavior may
  202.   --  change in the future so that the actual parsing (and execution of DDL
  203.   --  statement) do not occur until the cursor is executed with "execute".
  204.   --  DO NOT RELY ON THE CURRENT TIMING OF THE ACTUAL PARSING!
  205.   --  Input parameters:
  206.   --    c
  207.   --      Cursor id number of the cursor in where to parse the statement.
  208.   --    statement
  209.   --      Statement to parse.
  210.   --    language_flag
  211.   --      Specifies behavior for statement. Valid values are v6, v7 and NATIVE.
  212.   --      v6 and v7 specifies behavior according to Version 6 and ORACLE7,
  213.   --      respectively. NATIVE specifies behavior according to the version
  214.   --      of the database the program is connected to.
  215.   --    
  216.   procedure bind_variable(c in integer, name in varchar2, value in number);
  217.   procedure bind_variable(c in integer, name in varchar2, 
  218.                           value in varchar2);
  219.   procedure bind_variable(c in integer, name in varchar2, value in varchar2,
  220.                           out_value_size in integer);
  221.   procedure bind_variable(c in integer, name in varchar2, value in date);
  222.   procedure bind_variable(c in integer, name in varchar2, 
  223.                           value in mlslabel);
  224.   procedure bind_variable_char(c in integer, name in varchar2,
  225.                                value in char);
  226.   procedure bind_variable_char(c in integer, name in varchar2,
  227.                                value in char, out_value_size in integer);
  228.   procedure bind_variable_raw(c in integer, name in varchar2,
  229.                               value in raw);
  230.   procedure bind_variable_raw(c in integer, name in varchar2,
  231.                               value in raw, out_value_size in integer);
  232.   procedure bind_variable_rowid(c in integer, name in varchar2,
  233.                               value in rowid);
  234.   --  Bind the given value to the variable identified by its name
  235.   --  in the parsed statement in the given cursor.
  236.   --  If the variable is an in or in/out variable, the given bind value
  237.   --  should be a valid one.  If the variable is an out variable, the
  238.   --  given bind value is ignored.
  239.   --  Input parameters:
  240.   --    c
  241.   --      Cursor id number of the cursor to bind.
  242.   --    name
  243.   --      Name of the variable in the statement.
  244.   --    value
  245.   --      Value to bind to the variable in the cursor.
  246.   --      If the variable is an out or in/out variable, its type is the same
  247.   --      as the type of the value being passed in for this parameter.
  248.   --    out_value_size
  249.   --      Maximum expected out value size in bytes for the varchar2
  250.   --      out or in/out variable.  If it is not given for the varchar2
  251.   --      out or in/out variable, the size is the length of the current
  252.   --      "value".
  253.   --
  254.   procedure define_column(c in integer, position in integer, column in number);
  255.   procedure define_column(c in integer, position in integer, 
  256.                           column in varchar2,
  257.                           column_size in integer);
  258.   procedure define_column(c in integer, position in integer, column in date);
  259.   procedure define_column(c in integer, position in integer, 
  260.                           column in mlslabel);
  261.   procedure define_column_char(c in integer, position in integer,
  262.                                column in char, column_size in integer);
  263.   procedure define_column_raw(c in integer, position in integer, 
  264.                               column in raw,
  265.                               column_size in integer);
  266.   procedure define_column_rowid(c in integer, position in integer,
  267.                                 column in rowid);
  268.   --  Define a column to be selected from the given cursor; so this
  269.   --  procedure is applicable only to SELECT cursors.
  270.   --  The column being defined is identified by its relative position as
  271.   --  it appears on the select list in the statement in the given cursor.
  272.   --  The type of the column to be defined is the type of the value
  273.   --  being passed in for parameter "column".
  274.   --  Input parameters:
  275.   --    c
  276.   --      Cursor id number of the cursor to define the row to be selected.
  277.   --    position
  278.   --      Position of the column in the row being defined.
  279.   --    column
  280.   --      Type of the value being passed in for this parameter is
  281.   --      the type of the column to be defined.
  282.   --    column_size
  283.   --      Maximum expected size of the value in bytes for the
  284.   --      varchar2 column.
  285.   --
  286.   function execute(c in integer) return integer;
  287.   --  Execute the given cursor and return the number of rows processed
  288.   --  (valid and meaningful only for INSERT, DELETE or UPDATE statements;
  289.   --  for other types of statements, the return value is undefined and
  290.   --  should be ignored).
  291.   --  Input parameters:
  292.   --    c
  293.   --      Cursor id number of the cursor to execute.
  294.   --  Return value:
  295.   --    Number of rows processed if the statement in the cursor is
  296.   --    either an INSERT, DELETE or UPDATE statement or undefined otherwise.
  297.   --
  298.   function fetch_rows(c in integer) return integer;
  299.   --  Fetch rows from the given cursor. The function tries to fetch a
  300.   --  row. As long as "fetch_rows" is able to fetch a
  301.   --  row, it can be called repeatedly to fetch additional rows. If no
  302.   --  row was actually fetched, "fetch_rows"
  303.   --  cannot be called to fetch additional rows.
  304.   --  Input parameters:
  305.   --    c
  306.   --      Cursor id number of the cursor to fetch.
  307.   --  Return value:
  308.   --    The number of rows actually fetched.
  309.   --
  310.   function execute_and_fetch(c in integer, exact in boolean default false) 
  311.   return integer;
  312.   --  Execute the given cursor and fetch rows. Gives the same functionality
  313.   --  as a call to "execute" 
  314.   --  followed by a call to "fetch_rows". However, this function can 
  315.   --  potentially cut down on the number of message round-trips compared to
  316.   --  calling "execute" and "fetch_rows" separately.
  317.   --  Input parameters:
  318.   --    c
  319.   --      Cursor id number of the cursor to execute and fetch.
  320.   --    exact 
  321.   --      Raise an exception if the number of rows matching the query 
  322.   --      differs from one.
  323.   --  Return value:
  324.   --    The number of rows actually fetched.
  325.   --    
  326.   procedure column_value(c in integer, position in integer, value out number);
  327.   procedure column_value(c in integer, position in integer, 
  328.                          value out varchar2);
  329.   procedure column_value(c in integer, position in integer, value out date);
  330.   procedure column_value(c in integer, position in integer, 
  331.                          value out mlslabel);
  332.   procedure column_value_char(c in integer, position in integer,
  333.                               value out char);
  334.   procedure column_value_raw(c in integer, position in integer, value out raw);
  335.   procedure column_value_rowid(c in integer, position in integer, 
  336.                                value out rowid);
  337.   procedure column_value(c in integer, position in integer, value out number,
  338.                          column_error out number, actual_length out integer);
  339.   procedure column_value(c in integer, position in integer, 
  340.                          value out varchar2, column_error out number,
  341.                          actual_length out integer);
  342.   procedure column_value(c in integer, position in integer, value out date,
  343.                          column_error out number, actual_length out integer);
  344.   procedure column_value(c in integer, position in integer, 
  345.                          value out mlslabel, column_error out number,
  346.                          actual_length out integer);
  347.   procedure column_value_char(c in integer, position in integer,
  348.                               value out char, column_error out number, 
  349.                               actual_length out integer);
  350.   procedure column_value_raw(c in integer, position in integer, value out raw,
  351.                              column_error out number, 
  352.                              actual_length out integer);
  353.   procedure column_value_rowid(c in integer, position in integer, 
  354.                              value out rowid, column_error out number,
  355.                              actual_length out integer);
  356.   --  Get a value of the column identified by the given position
  357.   --  and the given cursor. This procedure is used to access the data 
  358.   --  retrieved by "fetch_rows".
  359.   --  Input parameters:
  360.   --    c
  361.   --      Cursor id number of the cursor from which to get the value.
  362.   --    position
  363.   --      Position of the column of which to get the value.
  364.   --  Output parameters:
  365.   --    value
  366.   --      Value of the column.  
  367.   --    column_error
  368.   --      Any column error code associated with "value".
  369.   --    actual_length
  370.   --      The actual length of "value" in the table before any truncation
  371.   --      during the fetch.
  372.   --  Exceptions:
  373.   --    inconsistent_type (ORA-06562)
  374.   --      Raised if the type of the given out parameter "value" is
  375.   --      different from the actual type of the value.  This type was
  376.   --      the given type when the column was defined by calling procedure
  377.   --      "define_column".
  378.   --
  379.   procedure variable_value(c in integer, name in varchar2,
  380.                            value out number);
  381.   procedure variable_value(c in integer, name in varchar2,
  382.                            value out varchar2);
  383.   procedure variable_value(c in integer, name in varchar2,
  384.                            value out date);
  385.   procedure variable_value(c in integer, name in varchar2,
  386.                            value out mlslabel);
  387.   procedure variable_value_char(c in integer, name in varchar2,
  388.                                 value out char);
  389.   procedure variable_value_raw(c in integer, name in varchar2,
  390.                                value out raw);
  391.   procedure variable_value_rowid(c in integer, name in varchar2,
  392.                                  value out rowid);
  393.   --  Get a value or values of the variable identified by the name
  394.   --  and the given cursor.  
  395.   --  Input parameters:
  396.   --    c
  397.   --      Cursor id number of the cursor from which to get the value.
  398.   --    name
  399.   --      Name of the variable of which to get the value.
  400.   --  Output parameters:
  401.   --    value
  402.   --      Value of the variable.  
  403.   --  Exceptions:
  404.   --    inconsistent_type (ORA-06562)
  405.   --      Raised if the type of the given out parameter "value" is
  406.   --      different from the actual type of the value.  This type was
  407.   --      the given type when the variable was bound by calling procedure
  408.   --      "bind_variable".
  409.   --
  410.   function last_error_position return integer;
  411.   function last_sql_function_code return integer;
  412.   function last_row_count return integer;
  413.   function last_row_id return rowid;
  414.   --  Get various information for the last-operated cursor in the session.
  415.   --  To ensure that the information relates to a particular cursor,
  416.   --  the functions should be called after an operation on that cursor and
  417.   --  before any other operation on any other cursor.
  418.   --  Return value:
  419.   --    last_error_position
  420.   --      Relative position in the statement when the error occurs.
  421.   --    last_sql_function_code
  422.   --      SQL function code of the statement. See list in OCI manual.
  423.   --    last_row_count
  424.   --      Cumulative count of rows fetched.
  425.   --    last_row_id
  426.   --      Rowid of the last processed row.
  427.   --
  428.  ------------
  429.   --  EXAMPLES
  430.   --
  431.   --  create or replace procedure copy(source in varchar2,
  432.   --                                   destination in varchar2) is
  433.   --    --  This procedure copies rows from a given source table to
  434.   --        a given destination table assuming that both source and destination
  435.   --    --  tables have the following columns:
  436.   --    --    - ID of type NUMBER,
  437.   --    --    - NAME of type VARCHAR2(30),
  438.   --    --    - BIRTHDATE of type DATE.
  439.   --    id number;
  440.   --    name varchar2(30);
  441.   --    birthdate date;
  442.   --    source_cursor integer;
  443.   --    destination_cursor integer;
  444.   --    rows_processed integer;
  445.   --  begin
  446.   --    -- prepare a cursor to select from the source table
  447.   --    source_cursor := dbms_sql.open_cursor;
  448.   --    dbms_sql.parse(source_cursor,
  449.   --                   'select id, name, birthdate from ' || source);
  450.   --    dbms_sql.define_column(source_cursor, 1, id);
  451.   --    dbms_sql.define_column(source_cursor, 2, name, 30);
  452.   --    dbms_sql.define_column(source_cursor, 3, birthdate);
  453.   --    rows_processed := dbms_sql.execute(source_cursor);
  454.   --
  455.   --    -- prepare a cursor to insert into the destination table
  456.   --    destination_cursor := dbms_sql.open_cursor;
  457.   --    dbms_sql.parse(destination_cursor,
  458.   --                   'insert into ' || destination ||
  459.   --                   ' values (:id, :name, :birthdate)');
  460.   --
  461.   --    -- fetch a row from the source table and
  462.   --    -- insert it into the destination table
  463.   --    loop
  464.   --      if dbms_sql.fetch_rows(source_cursor)>0 then
  465.   --        -- get column values of the row
  466.   --        dbms_sql.column_value(source_cursor, 1, id);
  467.   --        dbms_sql.column_value(source_cursor, 2, name);
  468.   --        dbms_sql.column_value(source_cursor, 3, birthdate);
  469.   --        -- bind the row into the cursor which insert
  470.   --        -- into the destination table
  471.   --        dbms_sql.bind_variable(destination_cursor, 'id', id);
  472.   --        dbms_sql.bind_variable(destination_cursor, 'name', name);
  473.   --        dbms_sql.bind_variable(destination_cursor, 'birthdate', birthdate);
  474.   --        rows_processed := dbms_sql.execute(destination_cursor);
  475.   --      else
  476.   --        -- no more row to copy
  477.   --        exit;
  478.   --      end if;
  479.   --    end loop;
  480.   --
  481.   --    -- commit and close all cursors
  482.   --    commit;
  483.   --    dbms_sql.close_cursor(source_cursor);
  484.   --    dbms_sql.close_cursor(destination_cursor);
  485.   --  exception
  486.   --    when others then
  487.   --      if dbms_sql.is_open(source_cursor) then
  488.   --        dbms_sql.close_cursor(source_cursor);
  489.   --      end if;
  490.   --      if dbms_sql.is_open(destination_cursor) then
  491.   --        dbms_sql.close_cursor(destination_cursor);
  492.   --      end if;
  493.   --      raise;
  494.   --  end;
  495.   --
  496.   procedure column_value_long(c in integer, position in integer, 
  497.                               length in integer, offset in integer,
  498.                               value out varchar2, value_length out integer);
  499.   --  Get (part of) the value of a long column.
  500.   --  Input parameters:
  501.   --    c
  502.   --      Cursor id number of the cursor from which to get the value.
  503.   --    position
  504.   --      Position of the column of which to get the value.
  505.   --    length
  506.   --      Number of bytes of the long value to fetch.
  507.   --    offset
  508.   --      Offset into the long field for start of fetch. 
  509.   --  Output parameters:
  510.   --    value
  511.   --      Value of the column as a varchar2.
  512.   --    value_length
  513.   --      The number of bytes actually returned in value.
  514.   --
  515.   procedure define_column_long(c in integer, position in integer);
  516.   --  Define a column to be selected from the given cursor; so this
  517.   --  procedure is applicable only to SELECT cursors.
  518.   --  The column being defined is identified by its relative position as
  519.   --  it appears on the select list in the statement in the given cursor.
  520.   --  The type of the column to be defined is the type LONG.
  521.   --  Input parameters:
  522.   --    c
  523.   --      Cursor id number of the cursor to define the row to be selected.
  524.   --    position
  525.   --      Position of the column in the row being defined.
  526.   --
  527.  
  528.  
  529. end;
  530. /
  531.  
  532. drop public synonym dbms_sql
  533. /
  534. create public synonym dbms_sql for sys.dbms_sql
  535. /
  536.  
  537. grant execute on dbms_sql to public
  538. /
  539.